/****************************************************************************
 Copyright (c) 2017-2018 SuperSuRaccoon
 
 Site: http://www.supersuraccoon-cocos2d.com
 Mail: supersuraccoon@gmail.com

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

const ssr = require('../namespace/SVGNamespace');

ssr.SVG.Util.Path = function() {};

/*
	M 1.2.3.4.5.6 => M 1.2 0.3 0.4 0.5 0.6
*/
ssr.SVG.Util.Path._checkDecimalTerms = function(terms) {
	let outputArray = [];
	for (let i = 0; i < terms.length; i++) {
		let decimalArray = terms[i].split(".");
		if (decimalArray.length > 1) {
			outputArray.push(parseFloat((decimalArray[0] ? decimalArray[0] : "0") + "." + decimalArray[1]));
			//
			for (let j = 2; j < decimalArray.length; j ++) {
				outputArray.push(parseFloat("0." + decimalArray[j]));
			}
		}
		else {
			outputArray.push(parseFloat(terms[i]));
		}
	}
	return outputArray;
};

ssr.SVG.Util.Path._fuzzyEquals = function fuzzyEquals(param1, param2, variance) {
	if (param1.x - variance <= param2.x && param2.x <= param1.x + variance) {
		if (param1.y - variance <= param2.y && param2.y <= param1.y + variance) {
			return true;
		}
	}
	return false;
};

// M moveto (x y)+
ssr.SVG.Util.Path.M = function(index, lastM, cmd, terms, configuration, outArray) {
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	let px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
	if (preStroke) {
		if (preStroke.commandType != ssr.SVG.Const.PATH_END) {
			let lastMStrokeobject = outArray[lastM - 1];
			if (preStroke.params.x != lastMStrokeobject.params.x || preStroke.params.y != lastMStrokeobject.params.y) {
				// implicit l+z
				let strokeObject1 = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + "(l)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
				outArray.push(strokeObject1);
				let strokeObject2 = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, cmd + "(z)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
				outArray.push(strokeObject2);
			}
			else {
				// implicit m+z
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, cmd + "(z)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
				outArray.push(strokeObject);
			}
		}
		else {
			preStroke.renderType = ssr.SVG.Const.RENDER_CLOSE;
		}
	}
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 2; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let x = parseFloat(terms[i * 2]), y = parseFloat(terms[i * 2 + 1]);
		if (cmd == "m") {
			x += px, y += py;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		if (i == 0) {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_MOVE, ssr.SVG.Const.RENDER_MOVE, cmd + " " + terms[i * 2] + " " + terms[i * 2 + 1], {"px": 0, "py": 0, "x": x, "y": y}, [{"x": x, "y": y}]);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
		else {
			// implict lineto command
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + "(l) " + terms[i * 2] + " " + terms[i * 2 + 1], {"px": px, "py": py, "x": x, "y": y}, [{"x": x, "y": y}]);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// L lineto (x y)+
ssr.SVG.Util.Path.L = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 2; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let x = parseFloat(terms[i * 2]), y = parseFloat(terms[i * 2 + 1]);
		if (cmd == "l") {
			x += px, y += py;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		if (configuration.segmentationOn) {
			let segmentArray = ssr.SVG.Util.Polyline.FromLine(px, py, x, y, configuration.segments);
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + "(*) " + segmentArray[m].x + " " + segmentArray[m].y, {"px": segmentArray[m - 1].x, "py": segmentArray[m - 1].y, "x": x, "y": y}, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + " " + terms[i * 2] + " " + terms[i * 2 + 1], {"px": px, "py": py, "x": x, "y": y}, [{"x": x, "y": y}]);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// H horizontal lineto x+
ssr.SVG.Util.Path.H = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i <= terms.length - 1; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let x = parseFloat(terms[i]);
		let y = py;
		if (cmd == "h") {
		 	x += px;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		if (configuration.segmentationOn) {
			let segmentArray = ssr.SVG.Util.Polyline.FromLine(px, py, x, y, configuration.segments);
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + " " + x + "(" + m + ")", {"px": segmentArray[m - 1].x, "py": segmentArray[m - 1].y, "x": x, "y": y}, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + " " + x, {"px": px, "py": py, "x": x, "y": y}, [{"x": x, "y": y}]);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// V vertical lineto y+
ssr.SVG.Util.Path.V = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i <= terms.length - 1; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let x = px;
		let y = parseFloat(terms[i]);
		if (cmd == "v") {
		 	y += py;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		if (configuration.segmentationOn) {
			let segmentArray = ssr.SVG.Util.Polyline.FromLine(px, py, x, y, configuration.segments);
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + " " + y + "(" + m + ")", {"px": segmentArray[m - 1].x, "py": segmentArray[m - 1].y, "x": x, "y": y}, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + " " + y, {"px": px, "py": py, "x": x, "y": y}, [{"x": x, "y": y}]);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// C curveto (x1 y1 x2 y2 x y)+
ssr.SVG.Util.Path.C = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	for (let i = 0; i < terms.length / 6; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let cx1 = parseFloat(terms[i * 6]), cy1 = parseFloat(terms[i * 6 + 1]);
		let cx2 = parseFloat(terms[i * 6 + 2]), cy2 = parseFloat(terms[i * 6 + 3]);
		let x = parseFloat(terms[i * 6 + 4]), y = parseFloat(terms[i * 6 + 5]);
		//
		if (cmd == "c") {
			cx1 += px, cy1 += py;
			cx2 += px, cy2 += py;
			x += px, y += py
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		cx1 = parseFloat(cx1.toFixed(3));
		cy1 = parseFloat(cy1.toFixed(3));
		cx2 = parseFloat(cx2.toFixed(3));
		cy2 = parseFloat(cy2.toFixed(3));
		//
		let instruction = cmd + " " + terms[i * 6] + " " + terms[i * 6 + 1] + " " + terms[i * 6 + 2] + " " + terms[i * 6 + 3] + " " + terms[i * 6 + 4] + " " + terms[i * 6 + 5];
		let params = {"px": px, "py": py, "cx1": cx1, "cy1": cy1, "cx2": cx2, "cy2": cy2, "x": x, "y": y};
		//
		let segmentArray = ssr.SVG.Util.Polyline.FromCubicBezier(px, py, cx1, cy1, cx2, cy2, x, y, configuration.segments);
		if (configuration.segmentationOn) {
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, instruction + "(" + m + ")", params, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_CURVE_C, ssr.SVG.Const.RENDER_POLYLINE, instruction, params, segmentArray);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// S smooth curveto	(x2 y2 x y)+
ssr.SVG.Util.Path.S = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 4; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let cx2 = parseFloat(terms[i * 4]), cy2 = parseFloat(terms[i * 4 + 1]);
		if (cmd == "s") {
			cx2 = px + cx2, cy2 = py + cy2;
		}
		let cx1 = px, cy1 = py;
		if (preStroke.commandType == ssr.SVG.Const.PATH_CURVE_C ||
			preStroke.commandType == ssr.SVG.Const.PATH_CURVE_S) {
			cx1 = 2 * px - preStroke.params.cx2;
			cy1 = 2 * py - preStroke.params.cy2;
		}
		let x = parseFloat(terms[i * 4 + 2]), y = parseFloat(terms[i * 4 + 3]);
		if (cmd == "s") {
			x += px, y += py;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		cx1 = parseFloat(cx1.toFixed(3));
		cy1 = parseFloat(cy1.toFixed(3));
		cx2 = parseFloat(cx2.toFixed(3));
		cy2 = parseFloat(cy2.toFixed(3));
		//
		let instruction = cmd + " " + terms[i * 4] + " " + terms[i * 4 + 1] + " " + terms[i * 4 + 2] + " " + terms[i * 4 + 3];
		let params = {"px": px, "py": py, "cx1": cx1, "cy1": cy1, "cx2": cx2, "cy2": cy2, "x": x, "y": y};
		//
		let segmentArray = ssr.SVG.Util.Polyline.FromCubicBezier(px, py, cx1, cy1, cx2, cy2, x, y, configuration.segments);
		if (configuration.segmentationOn) {
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, instruction + "(" + m + ")", params, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_CURVE_S, ssr.SVG.Const.RENDER_POLYLINE, instruction, params, segmentArray);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// Q Quadratic Bézier curveto (x1 y1 x y)+
ssr.SVG.Util.Path.Q = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 4; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let cx1 = parseFloat(terms[i * 4]), cy1 = parseFloat(terms[i * 4 + 1]);
		if (cmd == "q") {
			cx1 += px, cy1 += py;
		}
		let x = parseFloat(terms[i * 4 + 2]), y = parseFloat(terms[i * 4 + 3]);
		if (cmd == "q") {
			x += px, y += py;
		}
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		cx1 = parseFloat(cx1.toFixed(3));
		cy1 = parseFloat(cy1.toFixed(3));
		//
		let instruction = cmd + " " + terms[i * 4] + " " + terms[i * 4 + 1] + " " + terms[i * 4 + 2] + " " + terms[i * 4 + 3];
		let params = {"px": px, "py": py, "cx1": cx1, "cy1": cy1, "x": x, "y": y};
		//
		let segmentArray = ssr.SVG.Util.Polyline.FromQuadBezier(px, py, cx1, cy1, x, y, configuration.segments);
		if (configuration.segmentationOn) {
			for (var m = 0; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, instruction + "(" + m + ")", params, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_CURVE_Q, ssr.SVG.Const.RENDER_POLYLINE, instruction, params, segmentArray);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// T smooth quadratic Bézier curveto (x y)+
ssr.SVG.Util.Path.T = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 2; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		let x = parseFloat(terms[i * 2]), y = parseFloat(terms[i * 2 + 1]);
		if (cmd == "t") {
			x += px, y += py;
		}
		let cx1 = px, cy1 = py;
		if (preStroke.commandType == ssr.SVG.Const.PATH_CURVE_Q ||
			preStroke.commandType == ssr.SVG.Const.PATH_CURVE_T) {
			cx1 = 2 * px - preStroke.params.cx1;
			cy1 = 2 * py - preStroke.params.cy1;
		}
		//
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		cx1 = parseFloat(cx1.toFixed(3));
		cy1 = parseFloat(cy1.toFixed(3));
		//
		let instruction = cmd + " " + terms[i * 2] + " " + terms[i * 2 + 1];
		let params = {"px": px, "py": py, "cx1": cx1, "cy1": cy1, "x": x, "y": y};
		//
		let segmentArray = ssr.SVG.Util.Polyline.FromQuadBezier(px, py, cx1, cy1, x, y, configuration.segments);
		if (configuration.segmentationOn) {
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, instruction  + "(" + m + ")", params, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_CURVE_T, ssr.SVG.Const.RENDER_POLYLINE, instruction, params, segmentArray);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// A elliptical arc (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+
ssr.SVG.Util.Path.A = function(index, cmd, terms, configuration, outArray) {
	let px = 0, py = 0;
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	terms = ssr.SVG.Util.Path._checkDecimalTerms(terms);
	for (let i = 0; i < terms.length / 7; i++) {
		px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
		//
		let rx = parseFloat(terms[i * 7]), ry = parseFloat(terms[i * 7 + 1]), rotate = parseFloat(terms[i * 7 + 2]);
		let largeArcFlag = parseFloat(terms[i * 7 + 3]),  sweepFlag = parseFloat(terms[i * 7 + 4]);
		let x = parseFloat(terms[i * 7 + 5]), y = parseFloat(terms[i * 7 + 6]);
		if (cmd == "a") {
			x += px, y += py;
		}
		//
		x = parseFloat(x.toFixed(3));
		y = parseFloat(y.toFixed(3));
		//
		let instruction = cmd + " " + terms[i * 7] + " " + terms[i * 7 + 1] + " " + terms[i * 7 + 2] + " " + terms[i * 7 + 3] + " " + terms[i * 7 + 4] + " " + terms[i * 7 + 5] +  " " + terms[i * 7 + 6];
		let params = {"px": px, "py": py, "rx": rx, "ry": ry, "rotate": rotate, "largeArcFlag": largeArcFlag, "sweepFlag": sweepFlag, "x": x, "y": y};
		//
		let segmentArray = ssr.SVG.Util.Polyline.FromEllipticArc(px, py, rx, ry, rotate, largeArcFlag, sweepFlag, x, y, configuration.segments);
		if (configuration.segmentationOn) {
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, instruction  + "(" + m + ")", params, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
				preStroke = strokeObject;
			}
		}
		else {
			let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_CURVE_A, ssr.SVG.Const.RENDER_POLYLINE, instruction, params, segmentArray);
			outArray.push(strokeObject);
			preStroke = strokeObject;
		}
	}
};

// Z closepath (none)
ssr.SVG.Util.Path.Z = function(index, lastM, cmd, terms, configuration, outArray) {
	let lastMStrokeobject = outArray[lastM - 1];
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	let px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);

	if (ssr.SVG.Util.Path._fuzzyEquals(preStroke.params, lastMStrokeobject.params, cc.macro.FLT_EPSILON)) {
		let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, cmd, {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
		outArray.push(strokeObject);
		preStroke = strokeObject;
	}
	else {
		if (configuration.segmentationOn) {
			let segmentArray = ssr.SVG.Util.Polyline.FromLine(px, py, lastMStrokeobject.params.x, lastMStrokeobject.params.y, configuration.segments);
			for (var m = 1; m < segmentArray.length; m++) {
				let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, "z"  + "(" + m + ")", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": segmentArray[m].x, "y": segmentArray[m].y}]);
				outArray.push(strokeObject);
			}
			let strokeObject2 = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, cmd, {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
			outArray.push(strokeObject2);
			preStroke = strokeObject2;
		}
		else {
			let strokeObject1 = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_LINE, ssr.SVG.Const.RENDER_LINE, cmd + "(l)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}]);
			outArray.push(strokeObject1);
			let strokeObject2 = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, cmd, {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
			outArray.push(strokeObject2);
			preStroke = strokeObject2;
		}
	}
};

// check last implict z
ssr.SVG.Util.Path.Last = function(lastM, configuration, outArray) {
	let preStroke = (outArray.length > 0 ? outArray[outArray.length - 1] : null);
	if (preStroke.commandType == ssr.SVG.Const.PATH_END) {
		return;
	}
	let px = (preStroke ? preStroke.params.x : 0), py = (preStroke ? preStroke.params.y: 0);
	let lastMStrokeobject = outArray[lastM - 1];
	if (ssr.SVG.Util.Path._fuzzyEquals(lastMStrokeobject.params, preStroke.params, cc.macro.FLT_EPSILON)) {
		let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_CLOSE, "(z)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
		outArray.push(strokeObject);
	}
	else {
		let strokeObject = new ssr.SVG.Data.Stroke(ssr.SVG.Const.PATH_END, ssr.SVG.Const.RENDER_END, "(z)", {"px": px, "py": py, "x": lastMStrokeobject.params.x, "y": lastMStrokeobject.params.y}, [{"x": preStroke.params.x, "y": preStroke.params.y}]);
		outArray.push(strokeObject);
	}
};
